#####################################################
# Replication file for Santiago del Estero Province #
#####################################################


rm(list=ls(all=TRUE))
#setwd("")
library(eiPack)
library(coda)
library(foreign)

data=read.csv(file.path(getwd(),"data","SANTIAGO_data.csv"))


# Define row and column marginals of the transition matrix. 
GFCS=data$gral_541
GFP=data$gral_540
GPCS=data$gral_514
GO=data$gral_61+data$gral_324+data$gral_501
GN=data$votantes-GFCS-GFP-GPCS-GO


PFCS=data$paso_541
PFP=data$paso_540
PPCS=data$paso_514
PO=data$paso_61+data$paso_324+data$paso_501
PN=data$votantes-PFCS-PFP-PPCS-PO


data1=data.frame(PFCS,PFP,PPCS,PO,PN,GFCS,GFP,GPCS,GO,GN)


# Tunning the EI algorithm
tune.nocov=tuneMD(cbind(GFCS,GFP,GPCS,GO,GN)~cbind(PFCS,PFP,PPCS,PO,PN),
                  data=data1,ntunes=1,totaldraws=200000)



# MCMC
out.nocov=ei.MD.bayes(cbind(GFCS,GFP,GPCS,GO,GN)~cbind(PFCS,PFP,PPCS,PO,PN),
                      covariate=NULL,data=data1,tune.list=tune.nocov,verbose=0,ret.beta="r",thin=80,burnin=100000,ret.mcmc=TRUE) 


# Obtain transition matrix for each mesa: 
source(file.path(getwd(),"Codes","getbetas.R"))
beta11=getbetas(out.nocov,"PFCS","GFCS")
beta12=getbetas(out.nocov,"PFCS","GFP")
beta13=getbetas(out.nocov,"PFCS","GPCS")
beta14=getbetas(out.nocov,"PFCS","GO")
beta15=getbetas(out.nocov,"PFCS","GN")
beta21=getbetas(out.nocov,"PFP","GFCS")
beta22=getbetas(out.nocov,"PFP","GFP")
beta23=getbetas(out.nocov,"PFP","GPCS")
beta24=getbetas(out.nocov,"PFP","GO")
beta25=getbetas(out.nocov,"PFP","GN")
beta31=getbetas(out.nocov,"PPCS","GFCS")
beta32=getbetas(out.nocov,"PPCS","GFP")
beta33=getbetas(out.nocov,"PPCS","GPCS")
beta34=getbetas(out.nocov,"PPCS","GO")
beta35=getbetas(out.nocov,"PPCS","GN")
beta41=getbetas(out.nocov,"PO","GFCS")
beta42=getbetas(out.nocov,"PO","GFP")
beta43=getbetas(out.nocov,"PO","GPCS")
beta44=getbetas(out.nocov,"PO","GO")
beta45=getbetas(out.nocov,"PO","GN")
beta51=getbetas(out.nocov,"PN","GFCS")
beta52=getbetas(out.nocov,"PN","GFP")
beta53=getbetas(out.nocov,"PN","GPCS")
beta54=getbetas(out.nocov,"PN","GO")
beta55=getbetas(out.nocov,"PN","GN")
betaout=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)
write.table(betaout,file=file.path(getwd(),"Output","SGO_Mesa.txt"),col.names=NA)


# Obtain aggregate transition matrix. 
source(file.path(getwd(),"Codes","getbetasagr.R"))
beta11=getbetasagr(out.nocov,"PFCS","GFCS")
beta12=getbetasagr(out.nocov,"PFCS","GFP")
beta13=getbetasagr(out.nocov,"PFCS","GPCS")
beta14=getbetasagr(out.nocov,"PFCS","GO")
beta15=getbetasagr(out.nocov,"PFCS","GN")
beta21=getbetasagr(out.nocov,"PFP","GFCS")
beta22=getbetasagr(out.nocov,"PFP","GFP")
beta23=getbetasagr(out.nocov,"PFP","GPCS")
beta24=getbetasagr(out.nocov,"PFP","GO")
beta25=getbetasagr(out.nocov,"PFP","GN")
beta31=getbetasagr(out.nocov,"PPCS","GFCS")
beta32=getbetasagr(out.nocov,"PPCS","GFP")
beta33=getbetasagr(out.nocov,"PPCS","GPCS")
beta34=getbetasagr(out.nocov,"PPCS","GO")
beta35=getbetasagr(out.nocov,"PPCS","GN")
beta41=getbetasagr(out.nocov,"PO","GFCS")
beta42=getbetasagr(out.nocov,"PO","GFP")
beta43=getbetasagr(out.nocov,"PO","GPCS")
beta44=getbetasagr(out.nocov,"PO","GO")
beta45=getbetasagr(out.nocov,"PO","GN")
beta51=getbetasagr(out.nocov,"PN","GFCS")
beta52=getbetasagr(out.nocov,"PN","GFP")
beta53=getbetasagr(out.nocov,"PN","GPCS")
beta54=getbetasagr(out.nocov,"PN","GO")
beta55=getbetasagr(out.nocov,"PN","GN")

betaoutagr=data.frame(beta11,beta12,beta13,beta14,beta15,
                      beta21,beta22,beta23,beta24,beta25,
                      beta31,beta32,beta33,beta34,beta35,
                      beta41,beta42,beta43,beta44,beta45,
                      beta51,beta52,beta53,beta54,beta55,
                      data$votantes,data$prov,data$comuna,data$circ,data$mesa)

betaoutagr=as.vector(betaoutagr[1,1:100])
betaoutagr=matrix(betaoutagr,4)
betaoutagr_coefs=t(matrix(betaoutagr[1,],5))
betaoutagr_sd=t(matrix(betaoutagr[2,],5))
write.table(betaoutagr_coefs,file=file.path(getwd(),"Output","SGOAggregate_Coefs.txt"),col.names=NA)
write.table(betaoutagr_sd,file=file.path(getwd(),"Output","SGOAggregate_SD.txt"),col.names=NA)


#save(out.nocov, file = "SGOMCMC.Rdata") # Saves the MCMC runs


# Geweke Convergence Statistics:
geweke=geweke.diag(out.nocov$draws$Beta)
gewekestats=geweke[[1]]
gewekedf=data.frame(gewekestats)
#write.table(gewekedf,file="SGOgewekelist.txt",col.names=NA)

jpeg(file.path(getwd(),"Output","SGOHistogramGeweke.jpeg"))
hist(gewekestats)
dev.off()

print("Max Geweke")
print(max(gewekestats))
print("Min Geweke")
print(min(gewekestats))
print("Percentage Above 2.35")
print(length(gewekestats[abs(gewekestats)>2.35])/length(gewekestats))
print("Percentage Above 1.96")
print(length(gewekestats[abs(gewekestats)>1.96])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1.5])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1])/length(gewekestats))
print("Percentage Above 0.5")
print(length(gewekestats[abs(gewekestats)>0.5])/length(gewekestats))
